package com.sinodata.bsm.cicp.collector.informix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 由于Informix采集指标修改，该采集类用 OnlineLogCollector 采集类代替
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-1-8 下午9:27:05          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class LogCollector extends Collector {

    @Override
    public String[] getPropMeta() {
        // return new String[] { "PhysicalBufSiz", "PhysicalPagesIO", "PhysicalSize", "PhysicalStatus", "LogicalBufSiz", "LogicalPagesIO", "LogicalNum", "LogicalStatus" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "cmd" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] propMetas = this.getPropMeta();
        String cmd = this.getParamMap().get("cmd");
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue resPropertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] props = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmd, 0, 0);
            String strLine = null;
            List<String> list = new ArrayList<String>();
            while ((strLine = reader.readLine()) != null) {
                strLine = strLine.trim();
                if (strLine.contains("Physical Logging")) {
                    reader.readLine();
                    strLine = reader.readLine().trim();
                    String[] str = strLine.split("\\s+");
                    list.add(Double.parseDouble(str[2]) * 4 / 1024.0 + "");
                    list.add(Double.parseDouble(str[5]) * 4 / 1024.0 + "");
                    continue;
                }
                if (strLine.indexOf("physize") > -1) {
                    strLine = reader.readLine().trim();
                    String[] str = strLine.split("\\s+");
                    list.add(Double.parseDouble(str[1]) * 4 / 1024.0 + "");
                    if (list.size() == 3) {
                        list.add(getStatus(Double.parseDouble(list.get(1)) / Double.parseDouble(list.get(0))));
                    } else {
                        return null;
                    }
                    continue;
                }
                if (strLine.contains("Logical Logging")) {
                    reader.readLine();
                    strLine = reader.readLine().trim();
                    String[] str = strLine.split("\\s+");
                    list.add(Double.parseDouble(str[2]) * 4 / 1024.0 + "");
                    list.add(Double.parseDouble(str[7]) * 4 / 1024.0 + ""); //
                    continue;
                }
                if (strLine.trim().matches("\\d+\\s+active,\\s+\\d+\\s+total")) {
                    String[] str = strLine.split("\\s+");
                    list.add(str[2]);
                    if (list.size() == 7) {
                        list.add(getStatus(Double.parseDouble(list.get(5)) / Double.parseDouble(list.get(4))));
                    } else {
                        return null;
                    }
                }
            }
            for (int i = 0; i < propMetas.length; i++) {
                props[i] = ResPropertyValue.createPropValue(list.get(i), propMetas[i]);
            }
            resPropertyValueX.setValues(props);
            values.add(resPropertyValueX);
        } catch (IOException e) {
            logger.error("failed to collect informix logbuf", e);
            return null;
        }
        return values;
    }

    private String getStatus(double d) {
        if (d < 0.75) {
            return "太大";
        } else if (d < 0.9) {
            return "使用率较高";
        } else {
            return "太小";
        }
    }
}
